#
# toolchain build order: linux-api-headers->glibc->binutils->gcc->binutils->glibc
# NOTE: libtool requires rebuilt with each new gcc version
# http://www.linuxfromscratch.org/lfs/view/7.6/chapter06/gcc.html

pkgname=('gcc' 'gcc-libs') 
pkgver=7.3.0
_pkgver=7
_snapshot=6-20170607
_islver=0.18
pkgrel=1
pkgdesc="The GNU Compiler Collection"
arch=('x86_64')
license=('GPL' 'LGPL' 'FDL' 'custom')
url="https://gcc.gnu.org"
makedepends=('binutils>=2.30' 'libmpc' 'doxygen' 'autogen')
checkdepends=('dejagnu' 'inetutils')
options=('!libtool' '!emptydirs')
source=("https://mirrors.concertpass.com/gcc/releases/gcc-${pkgver}/gcc-${pkgver}.tar.xz"
#source=("ftp://gcc.gnu.org/pub/gcc/snapshots/${_snapshot}/gcc-${_snapshot}.tar.xz"
        "ftp://gcc.gnu.org/pub/gcc/infrastructure/isl-${_islver}.tar.bz2")
md5sums=('be2da21680f27624f3a87055c4ba5af2'
         '11436d6b205e516635b666090b94ab32')

  _basedir="gcc-${pkgver}"
  #_basedir="gcc-${_snapshot}"


build() {
  cd ${srcdir}/${_basedir}
  
  ln -s ../isl-${_islver} isl

  # Do not run fixincludes
  sed -i 's@\./fixinc\.sh@-c true@' gcc/Makefile.in
  
  sed -i '/m64=/s/lib64/lib/' gcc/config/i386/t-linux64
  
  sed -i "/ac_cpp=/s/\$CPPFLAGS/\$CPPFLAGS -O2/" {libiberty,gcc}/configure
  
  #patch -p1 -i ${srcdir}/isl-0.13.patch
  
  # using -pipe causes test-suite failures
  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=48565
  CFLAGS=${CFLAGS/-pipe/}
  CXXFLAGS=${CXXFLAGS/-pipe/}

  cd ${srcdir}
  mkdir -p gcc-build  
  cd gcc-build

  ${srcdir}/${_basedir}/configure --prefix=/usr \
      --libdir=/usr/lib \
      --libexecdir=/usr/lib \
      --mandir=/usr/share/man \
      --infodir=/usr/share/info \
      --with-bugurl=https://github.com/KaOSx/core/issues \
      --enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++ \
      --enable-shared \
      --enable-threads=posix \
      --enable-libmpx \
      --with-system-zlib \
      --with-isl \
      --enable-__cxa_atexit \
      --disable-libunwind-exceptions \
      --enable-clocale=gnu \
      --disable-libstdcxx-pch \
      --disable-libssp \
      --enable-gnu-unique-object \
      --enable-linker-build-id \
      --enable-lto \
      --enable-plugin \
      --enable-install-libiberty \
      --with-linker-hash-style=gnu \
      --enable-gnu-indirect-function \
      --disable-multilib \
      --disable-werror \
      --enable-checking=release 
  make

  cd $CHOST/libstdc++-v3
  make doc-man-doxygen
}

check() {
  cd ${srcdir}/gcc-build

  # http://gcc.gnu.org/bugzilla/show_bug.cgi?id=31827
  ulimit -s 32768

  make -k check || true
  ${srcdir}/${_basedir}/contrib/test_summary
}

package_gcc-libs()
{
  pkgdesc="Runtime libraries shipped by GCC"
  groups=('base')
  depends=('glibc>=2.26')
  options=('!emptydirs' '!strip')
  
  cd ${srcdir}/gcc-build

  make -C $CHOST/libgcc DESTDIR=${pkgdir} install-shared
  rm ${pkgdir}/usr/lib/gcc/$CHOST/$pkgver/libgcc_eh.a
  
  for lib in libatomic \
             libcilkrts \
             libgfortran \
             libgomp \
             libitm \
             libquadmath \
             libsanitizer/{a,l,ub}san \
             libstdc++-v3/src \
             libvtv; do
    make -C $CHOST/$lib DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  done
  
  make -C $CHOST/libsanitizer/tsan DESTDIR=${pkgdir} install-toolexeclibLTLIBRARIES
  
  make -C $CHOST/libobjc DESTDIR=${pkgdir} install-libs
  
  make -C $CHOST/libstdc++-v3/po DESTDIR=${pkgdir} install
  
  make -C $CHOST/libmpx DESTDIR=${pkgdir} install

  for lib in libgomp \
             libitm \
             libquadmath; do
    make -C $CHOST/$lib DESTDIR=${pkgdir} install-info
  done

  find ${pkgdir} -name *.a -delete
  
  install -Dm644 ${srcdir}/${_basedir}/COPYING.RUNTIME \
    ${pkgdir}/usr/share/licenses/gcc-libs/RUNTIME.LIBRARY.EXCEPTION
}

package_gcc()
{
  pkgdesc="The GNU Compiler Collection"
  depends=("gcc-libs=$pkgver-$pkgrel" 'binutils>=2.30' 'libmpc')
  groups=('base-devel')
  conflicts=('gcc-fortran'
             'gcc-objc'
             'gcc-ada'
             'gcc-go')
  replaces=('gcc-fortran'
            'gcc-objc'
            'gcc-ada'
            'gcc-go')
  provides=('gcc-fortran'
            'gcc-objc'
            'gcc-ada'
            'gcc-go')

  cd ${srcdir}/gcc-build
  make DESTDIR=${pkgdir} install 
  
  cd ${srcdir}/gcc-build/gcc
  make DESTDIR=$pkgdir ada.install-{common,info}

  ln -s gcc ${pkgdir}/usr/bin/cc
  ln -s gfortran ${pkgdir}/usr/bin/f95
  ln -s gcc ${pkgdir}/usr/bin/gnatgcc
  
  mv ${pkgdir}/usr/lib/gcc/$CHOST/$pkgver/adalib/libgna{rl,t}-${_pkgver}.so ${pkgdir}/usr/lib
  ln -s libgnarl-${_pkgver}.so ${pkgdir}/usr/lib/libgnarl.so
  ln -s libgnat-${_pkgver}.so ${pkgdir}/usr/lib/libgnat.so
  rm ${pkgdir}/usr/lib/gcc/$CHOST/$pkgver/adalib/libgna{rl,t}.so
  
  # remove unneeded static libs
  rm $pkgdir/usr/lib/libasan.a
  rm $pkgdir/usr/lib/libatomic.a
  rm $pkgdir/usr/lib/libcilkrts.a
  rm $pkgdir/usr/lib/libgfortran.a
  rm $pkgdir/usr/lib/libgo.a
  rm $pkgdir/usr/lib/libgomp.a
  rm $pkgdir/usr/lib/libitm.a
  rm $pkgdir/usr/lib/libtsan.a
  
  # remove gcc-libs provided
  rm $pkgdir/usr/lib/libasan.so*
  rm $pkgdir/usr/lib/libatomic.so*
  rm $pkgdir/usr/lib/libcilkrts.so*
  rm $pkgdir/usr/lib/libgcc*
  rm $pkgdir/usr/lib/libgfortran.so*
  rm $pkgdir/usr/lib/libgomp.so*
  #rm $pkgdir/usr/lib/libgomp-plugin*
  rm $pkgdir/usr/lib/libitm.so*
  rm $pkgdir/usr/lib/liblsan*
  rm $pkgdir/usr/lib/libmpx*
  rm $pkgdir/usr/lib/libobjc*
  rm $pkgdir/usr/lib/libquadmath*
  rm $pkgdir/usr/lib/libstdc++.so*
  rm $pkgdir/usr/lib/libtsan.so*
  rm $pkgdir/usr/lib/libubsan*
  #rm $pkgdir/usr/lib/libvtv*
  rm $pkgdir/usr/share/info/libgomp*
  rm $pkgdir/usr/share/info/libitm*
  rm $pkgdir/usr/share/info/libquadmath*
  rm $pkgdir/usr/share/locale/de/LC_MESSAGES/libstdc++.mo
  rm $pkgdir/usr/share/locale/fr/LC_MESSAGES/libstdc++.mo
  
  # use from go
  rm $pkgdir/usr/bin/go
  rm $pkgdir/usr/bin/gofmt

  # POSIX conformance launcher scripts for c89 and c99
  cat > $pkgdir/usr/bin/c89 <<"EOF"
#!/bin/sh
fl="-std=c89"
for opt; do
  case "$opt" in
    -ansi|-std=c89|-std=iso9899:1990) fl="";;
    -std=*) echo "`basename $0` called with non ANSI/ISO C option $opt" >&2
	    exit 1;;
  esac
done
exec gcc $fl ${1+"$@"}
EOF

  cat > $pkgdir/usr/bin/c99 <<"EOF"
#!/bin/sh
fl="-std=c99"
for opt; do
  case "$opt" in
    -std=c99|-std=iso9899:1999) fl="";;
    -std=*) echo "`basename $0` called with non ISO C99 option $opt" >&2
	    exit 1;;
  esac
done
exec gcc $fl ${1+"$@"}
EOF

  chmod 755 $pkgdir/usr/bin/c{8,9}9

  # install the libstdc++ man pages
  #make -C $CHOST/libstdc++-v3/doc DESTDIR=$pkgdir doc-install-man

  install -d ${pkgdir}/usr/share/licenses/gcc/
  ln -s ../gcc-libs/RUNTIME.LIBRARY.EXCEPTION ${pkgdir}/usr/share/licenses/gcc/
}

